package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.MyAddressAdapter;
import com.wnhz.work.jy.bean.ChooseAddressBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---我的地址
 */
public class MyAddressActivity extends BaseActivity implements View.OnClickListener {
    private final int FROM_VOCATION_CODE = 642;
    private final int FROM_COMPILE_CODE = 652;
    private TextView title;
    private ListView listView;
    private TextView tv_add_address;

    private List<ChooseAddressBean> chooseLise = new ArrayList<>();
    private MyAddressAdapter mAdapter;

    private String name;
    private String phone;
    private String city;
    private String area;
    private String doorplate;
    private String shen;
    private String shi;
    private String qu;

    @ViewInject(R.id.include_occupied)
    private View include_occupied;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_address);
        x.view().inject(this);

        initData();
        initView();

    }


    private void setBlank() {
        if (chooseLise.size() == 0) {
            include_occupied.setVisibility(View.VISIBLE);
        } else {
            include_occupied.setVisibility(View.GONE);
        }
    }

    private void initData() {

    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("我的地址");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_addAddress).setOnClickListener(this);
        findViewById(R.id.tv_add_address).setOnClickListener(this);

        listView = (ListView) findViewById(R.id.lv_my_address);
        include_occupied = findViewById(R.id.include_occupied);
        tv_add_address = (TextView) findViewById(R.id.tv_add_address);


        uploadByxUtils();


        mAdapter = new MyAddressAdapter(this, chooseLise, this);
        listView.setAdapter(mAdapter);
    }

    private void uploadByxUtils() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.ADDRESS_LIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                System.out.println(result + "我的地址");
                chooseLise.clear();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray jsonArray = jsonObject.optJSONArray("address");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        jsonObject = jsonArray.optJSONObject(i);
                        String name = jsonObject.optString("name");
                        String address = jsonObject.optString("address");
                        String province = jsonObject.optString("province");
                        String citys = jsonObject.optString("city");
                        String areas = jsonObject.optString("area");
                        String phoneNumber = jsonObject.optString("phoneNumber");
                        String addressId = jsonObject.optString("addressId");
                        String status = jsonObject.optString("status");

                        ChooseAddressBean bean = new ChooseAddressBean();
                        bean.setName(name);
                        bean.setAddress(address);
                        bean.setProvince(province);
                        bean.setCitys(citys);
                        bean.setAreas(areas);
                        bean.setPhone(phoneNumber);
                        bean.setAddressId(addressId);

                        chooseLise.add(bean);
                    }
                    setBlank();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        Integer index;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //新增地址
            case R.id.tv_addAddress:
                intent = new Intent(MyAddressActivity.this, AddAddressActivity.class);
                startActivityForResult(intent, FROM_VOCATION_CODE);
                break;

            //新增收货地址
            case R.id.tv_add_address:
                intent = new Intent(MyAddressActivity.this, AddAddressActivity.class);
                startActivityForResult(intent, FROM_VOCATION_CODE);
                break;

            //编辑
            case R.id.tv_compile:
                index = (Integer) v.getTag();

                intent = new Intent(MyAddressActivity.this, CompileAddressActivity.class);
                intent.putExtra("name", chooseLise.get(index).getName());
                intent.putExtra("phone", chooseLise.get(index).getPhone());
                intent.putExtra("city", chooseLise.get(index).getProvince() + chooseLise.get(index).getCitys() + chooseLise.get(index).getAreas());
                intent.putExtra("sheng", chooseLise.get(index).getProvince());
                intent.putExtra("shi", chooseLise.get(index).getCitys());
                intent.putExtra("qu", chooseLise.get(index).getAreas());
                intent.putExtra("area", chooseLise.get(index).getArea());
                intent.putExtra("doorplate", chooseLise.get(index).getDoorplate());
                intent.putExtra("ChooseAddressBean", chooseLise.get(index));
                intent.putExtra("index", index);
                intent.putExtra("addressId", chooseLise.get(index).getAddressId());
                startActivityForResult(intent, FROM_COMPILE_CODE);
                break;

            default:
                break;
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        int index;
        switch (requestCode) {
            case FROM_VOCATION_CODE:
                if (resultCode == RESULT_OK) {
                    name = data.getExtras().getString("name");
                    phone = data.getExtras().getString("phone");
                    city = data.getExtras().getString("city");
                    area = data.getExtras().getString("area");
                    doorplate = data.getExtras().getString("doorplate");

                    ChooseAddressBean bean = new ChooseAddressBean();
                    bean.setName(name);
                    bean.setPhone(phone);
                    bean.setCity(city);
                    bean.setArea(area);
                    bean.setDoorplate(doorplate);

                    chooseLise.add(bean);
                }
                break;

            case FROM_COMPILE_CODE:
                if (resultCode == RESULT_OK) {
                    name = data.getExtras().getString("name");
                    phone = data.getExtras().getString("phone");
                    city = data.getExtras().getString("city");
                    area = data.getExtras().getString("area");
                    doorplate = data.getExtras().getString("doorplate");
                    index = data.getExtras().getInt("index");

                    ChooseAddressBean bean = new ChooseAddressBean();
                    bean.setName(name);
                    bean.setPhone(phone);
                    bean.setCity(city);
                    bean.setArea(area);
                    bean.setDoorplate(doorplate);

                    chooseLise.add(bean);
                } else if (resultCode == 888) {
                    index = data.getExtras().getInt("index");
                    System.out.println(index + "=======index");
                    chooseLise.remove(index);
                }
                break;

            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        chooseLise.clear();
        mAdapter.notifyDataSetChanged();
        uploadByxUtils();
        setBlank();
        super.onResume();
    }

}
